package org.vectrics.skill;


import java.util.Iterator;

import org.apache.log4j.Logger;
import org.vectrics.SystemException;
import org.vectrics.common.db.RecordObject;
 
public abstract class SkillRating implements RecordObject, java.io.Serializable {
	private transient static Logger log = Logger.getLogger(SkillRating.class);
	
	private Long id = null;
	private Skill skill = null;
	private Boolean viewChildren = new Boolean(true);
	private String level = null;
	private SkillScoringMethod scoringMethod = null;

	public SkillRating() {
		super();
	}
	
    public long getScore() {
    	SkillScoringMethodLevel scoringLevel = this.getSkillScoringLevel();
    	return(scoringLevel.getScore().intValue());
    }
    
    public SkillScoringMethodLevel getSkillScoringLevel() {
    	Skill skill = getSkill();
    	if (skill == null)
    		throw new SystemException("Skill was not assigned to this rating");
    	Iterator iter = skill.getScoringLevels().iterator();
    	SkillScoringMethodLevel scoringLevel = null;
    	while (iter.hasNext()) {
    		SkillScoringMethodLevel currentLevel = (SkillScoringMethodLevel)iter.next();
    		if ((currentLevel.getLevelValue().equals(this.getLevel())) &&
    				(currentLevel.getSkillScoringMethod().getCode().equals(this.getScoringMethod().getCode())))
    			scoringLevel = currentLevel;
    	}
    	if (scoringLevel == null)
    		throw new SystemException("Could not find scoring level for user.  Skill = " + skill.getCode() 
    					+ "   level = " + this.getLevel() 
						+   "  Scoring Method: " + this.getScoringMethod().getCode());
    	return(scoringLevel);
    }


	/**
	 * @return Returns the level.
	 */
	public String getLevel() {
		return level;
	}
	/**
	 * @param level The level to set.
	 */
	public void setLevel(String level) {
		this.level = level;
/*
		SkillScoringMethodLevel methodLevel = this.getSkillScoringLevel();

		if ((methodLevel == null) && (false == this.getSkill().getScoringMethod().getCode().equals(SkillScoringMethod.SCORING_METHOD_NONE))) 
			throw new SystemException("Invalid level (" + level 
					+ ") - not found on list of valid values for scoring method for skill." 
					+ "\r\n - level = " + level 
					+ "\r\n - skill = " + this.skill.getCode() 
					+ "\r\n - scoring method = " + this.scoringMethod.getCode());
					*/
	}
	/**
	 * @return Returns the scoringMethod.
	 */
	public SkillScoringMethod getScoringMethod() {
		return scoringMethod;
	}
	/**
	 * @param scoringMethod The scoringMethod to set.
	 */
	public void setScoringMethod(SkillScoringMethod scoringMethod) {
		this.scoringMethod = scoringMethod;
	}
	
    public abstract boolean hasSkill();
    /*
	{
    	if (((getLevel() != null) && (getLevel().intValue() > 0)) 
    	        || ((getYears() != null) && (getYears().intValue() > 0))
    	        || ((getYes() != null) && (getYes().booleanValue())) 
    	        || ((getViewChildren() != null) && (getViewChildren().booleanValue())) 
    	        || ((getWeight() != null) && (getWeight().intValue() > 0))
    	        )
    		return(true);
    	else
    		return(false);
    };
    */
    
    public String getLevelDescription(int level) {
    	String desc = "Undefined level description";
    	return(desc);
    }
    
    public String getDescription() {
		String desc = this.getSkillScoringLevel().getCaption();
		return(desc);    	
    }

	public Skill getSkill() {
		return skill;
	}


	public void setSkill(Skill skill) {
		this.skill = skill;
		this.scoringMethod = skill.getScoringMethod();
	}


	public Boolean getViewChildren() {
		return viewChildren;
	}

	public void setViewChildren(Boolean viewChildren) {
		this.viewChildren = viewChildren;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}


}